﻿
$(document).ready(function () {
    DoubleScroll();
    AddLesson();
});


$(function () {
    $('.se-edit').click(function (e) {
        var row = $(this).parent().parent();
        var lessonId = row.attr('id');
        //var table = row.parent();
        var table = $('.schedule-edit');

        $.ajax({
            type: 'GET',
            url: '/Schedule/EditLessonInline/' + lessonId,
            dataType: "html",
            success: function (data)
            {
                table.prepend(data);
                DateTimePicker();
                Submit();
                Abort();
                UpdatePeriodicitymask();
                $('#' + lessonId).animateHighlight('#92E150', '#FFFFF0', 800);
                
            }
        });

        //remove row
        //$(this).parent().empty().append(code);
    });
});


var UpdatePeriodicitymask = function() {
    $("#periodicity").change( function (e) {
        var t = e.target || e.srcElement;
        var val = t.value;
        var pattern = /^\d{1,32}$/g;
        if (pattern.test(val)) {
            var row = $(this).parent().parent();
            var maskCell = $(row).children(".se-periodicity-mask-edit");
            var currentCount = maskCell.children("input[type=checkbox]").length;
            var diff = currentCount - val;
            if (diff < 0) {
                for (var i = currentCount; i < val; i++) {
                    maskCell.append('<input checked="checked" class="check-box" data-val="true" data-val-required="The Boolean field is required." id="Periodicitymask_' + i + '_" name="Periodicitymask[' + i + ']" type="checkbox" value="true">');
                }
            }
            if (diff > 0) {
                var checkBoxes = maskCell.children('input[type=checkbox]');
                alert(checkBoxes.length);
                for (var j = val; j < currentCount; j++) {
                    $(checkBoxes[j]).hide();
                }
            }
        }
    }
    )
}

var AddLesson = function() {
    $('.se-newlesson').click(function() {
        //var table = $('.schedule-edit').parent().parent();
        var table = $('.se-lesson-edit');

        $.ajax({
            type: 'GET',
            url: '/Schedule/AddLessonInline/',
            dataType: "html",
            success: function (data) {
                table.prepend(data);
                DateTimePicker();
                Submit();
                Abort();
                UpdatePeriodicitymask();
            }
        });

    }
    );
};

var DoubleScroll = function () {
    $(".doublescroll")
        .before($("<div></div>")
            .append($("<div></div>")
                .css({
                    "padding-top": "1px",
                    "width": $(".doublescroll")[0].scrollWidth + "px"
                })
                .text('\xA0')
            )
            .css({
                "overflow": "auto",
                "overflowY": "hidden"
            }).
            bind("scroll", function () {
                $(".doublescroll")[0].scrollLeft = $(this)[0].scrollLeft;
            })
        )
        .bind("scroll", function () {
            $(this).prev()[0].scrollLeft = $(this)[0].scrollLeft;
        });
};

var Submit = function () {
    $('.se-submit').click(function () {
        var row = $(this).parent().parent();
        row.hide();

    });
};

var Abort = function() {
    $('.se-abort').click(function() {
        var lessonId = $(this).parent().parent().attr('id');
        var row = $(this).parent().parent();
        row.hide();

        $.ajax({
            type: 'GET',
            url: '/Schedule/EditLessonAbort/' + lessonId,
            dataType: "html",
            success: function(data) {
            }
        });


    });
};

var OnLessonAdd = function (lessonId) {
    $('.' + lessonId).animateHighlight('rgb(0,255,0)', 'rgb(230,230,230)', 1000);
};
    







/*
$(function () {
    $('.weer').click(function (e) {
        //ловим элемент, по которому кликнули
        var t = e.target || e.srcElement;
        //получаем название тега
        var elmName = t.tagName.toLowerCase();
        //если это инпут - ничего не делаем
        if (elmName == 'input') { return false; }
        var oldVal = $(this).html();
        var code = '<input type="text" id="j-cell-mark-edit" value="' + oldVal + '" data-studentid="' + $(this).parent().data("studentid") + '"  data-subjectid="' + $(this).parent().data("subjectid") + '"  data-date="' + $(this).parent().data("date") + '"/>';
        $(this).empty().append(code);
        $('#j-cell-mark-edit').focus();
        $('#j-cell-mark-edit').blur(function () {
            var val = $(this).val();
            var pattern = /^\d+$/g;
            if (pattern.test(val) || val == "") {
                $(this).parent().empty().html(val);

                if (val == "") {
                    val = "-1"
                };
                var formData = {
                    "studentid": $(this).data("studentid")
                    , "subjectid": $(this).data("subjectid")
                    , "date": $(this).data("date")
                    , "mark": val
                };

                $.ajax({
                    url: '/Journal/SetMark'
                    , type: 'POST'
                    , data: 'jsonData=' + $.toJSON(formData)
                    , success: function (res) {

                    }
                });
            }
            else {
                $(this).parent().empty().html(oldVal);
            };

        });
    });
});

*/